package com.yiwei.jmx.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.LinearSnapHelper;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.allen.library.SuperButton;
import com.yiwei.jmx.R;
import com.yiwei.jmx.adapter.MyBorrowRecordAdapter;
import com.yiwei.jmx.api.Api;
import com.yiwei.jmx.base.BaseActivity;
import com.yiwei.jmx.event.ChangeHomePageEvent;
import com.yiwei.jmx.util.Network;
import com.yiwei.jmx.util.RxLifeUtil;
import com.yiwei.jmx.util.ToastUtil;
import com.yiwei.jmx.view.V19FrameLayout;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by Administrator on 2018/6/10.
 */

public class MyBorrowRecordActivity extends BaseActivity implements MyBorrowRecordAdapter.OnChooseListener {
    @BindView(R.id.btn_back)
    ImageView btnBack;
    @BindView(R.id.txt_title)
    TextView txtTitle;
    @BindView(R.id.txt_right)
    TextView txtRight;
    @BindView(R.id.layout_title)
    V19FrameLayout layoutTitle;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.nothing)
    TextView nothing;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    @BindView(R.id.btn_apply)
    SuperButton btnApply;
    private Unbinder unbinder;
    private MyBorrowRecordAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my_borrow_record);
        initBar();
        unbinder = ButterKnife.bind(this);
        init();
    }

    private void init() {
        txtTitle.setText("我的借款");
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                getMyBorrowList();
            }
        });
        adapter = new MyBorrowRecordAdapter(this);
        adapter.setOnChooseListener(this);
//        LinearSnapHelper linearSnapHelper = new LinearSnapHelper();
//        linearSnapHelper.attachToRecyclerView(list);
        list.setAdapter(adapter);
        getMyBorrowList();
    }

    public void getMyBorrowList() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.makeText(this, "网络连接失败").show();
            return;
        }
        refresh.setRefreshing(true);

        Api.api_service.getDealListV3().compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);

                    } catch (NullPointerException e) {

                    }

                }).
                subscribe(result -> {
                    adapter.update(result.data);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbinder.unbind();
        unbinder = null;
    }


    @Override
    public void onClickHetong(String id) {
        Intent intent = new Intent(this, LoanAgreementActivity.class);
        intent.putExtra("type", 2);
        intent.putExtra("deal_id", id);
        startActivity(intent);
        overridePendingTransition(R.anim.right_enter, R.anim.left_out);
    }

    @Override
    public void onClickCheck(String status, String id) {
//        Util.startActivity(this, WechatChatActivity.class);
        // 0 - 申请中，1 - 招标中，2 - 待放款，3 - 审核失败，4 - 还款中，5 - 已还清
        switch (status) {
            case "0":
            case "1":
            case "2":
            case "3":
                Intent intent = new Intent(this, ApplyStatusIngActivity.class);
                intent.putExtra("deal_id", id);
                startActivity(intent);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case "4":
                Intent intent1 = new Intent(this, RepayMoneyActivity.class);
                intent1.putExtra("deal_id", id);
                startActivity(intent1);
                overridePendingTransition(R.anim.right_enter, R.anim.left_out);
                break;
            case "5":
                EventBus.getDefault().post(new ChangeHomePageEvent(0));
                finish();
                break;
        }


    }


    @OnClick({R.id.btn_back, R.id.btn_apply})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_back:
                finish();
                break;
            case R.id.btn_apply:
                EventBus.getDefault().post(new ChangeHomePageEvent(0));
                finish();
                break;
        }
    }
}
